package com.cy.service;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.webservice.SoapClient;
import com.cy.config.Config;
import com.cy.domain.VehicleAlarmInfo;
import com.cy.domain.ViolationVehicle;
import com.cy.utils.DateUtils;
import com.cy.utils.ImageUtil;
import com.cy.utils.TextUtil;
import com.cy.utils.VehicleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;

import javax.xml.xpath.XPathConstants;
import java.util.Map;

/**
 * @Author jiangwang
 * @Created by 2021/12/16 9:16
 * @Description: TODO
 */
@Service
public class SendMessage
{
    private static final Logger logger = LoggerFactory.getLogger(SendMessage.class);

    /**
     * 发送数据到海康平台
     *
     * @param vv
     */
    public void sendMessage(ViolationVehicle vv)
    {
        try
        {
            logger.info("开始上传数据到海康平台，违法车牌号为：{}，违法时间为：{}", vv.getPlateNbr(), vv.getCaptureTime());
            VehicleAlarmInfo hk = new VehicleAlarmInfo();
            //卡口编号
            hk.setKKBH(Config.kkbHMap.get(vv.getDeviceNo()));
            //卡口秘钥
            hk.setKKMY(Config.kkmYMap.get(vv.getDeviceNo()));
            //经过时刻
            hk.setJGSK(DateUtils.stringTimeFormat(vv.getCaptureTime()));
            //车道编号
            hk.setCDBH("".equals(String.valueOf(vv.getLane())) ? "01" : String.valueOf(vv.getLane()));
            //号牌号码
            hk.setHPHM(vv.getPlateNbr());
            //号牌颜色
            hk.setHPYS("".equals(vv.getPlateColor()) ? "4" : vv.getPlateColor());
            //号牌种类
            hk.setHPZL(VehicleUtil.coverToPlateType(vv.getPlateColor()));
            //车辆速度
            hk.setCLSD(String.valueOf(vv.getVehicleSpeed()));
            //车外廓长
            hk.setCWKC("0");
            //车身颜色
            hk.setCSYS("Z");
            //车辆类型
            hk.setCLLX(VehicleUtil.convertToVehicleType(vv.getVehicleType()));
            //违法类型代码
            if (vv.getViolationBehaviors() != null && vv.getViolationBehaviors().length > 0)
            {
                hk.setWFLX(vv.getViolationBehaviors()[0]);
            }
            else
            {
                logger.info("违法代码为空！！");
                return;
            }
            //实时与否
            hk.setSSYF("0");
            //车辆主品牌
            hk.setCLZPP1("");
            //车辆子品牌
            hk.setCLZPP2("");
            //车辆年款
            hk.setCLNK("");
            //主驾驶安全带状态
            hk.setZJSAQDZT("");
            //副驾驶安全带状态
            hk.setFJSAQDZT("");
            //主驾驶遮阳板状态
            hk.setZJSZYBZT("");
            //副驾驶遮阳板状态
            hk.setFJSZYBZT("");
            //是否打电话
            hk.setSFDDH("");
            //是否黄标车
            hk.setSFHBC("");
            //是否危险品车
            hk.setSFWXPC("");
            //是否有挂件
            hk.setSFYGJ("");
            //行驶方向
            hk.setCDFX("");

            String carPic1 = null;
            String carPic2 = null;
            Map<String, String> ext = vv.getExtendedProperties();
            if (ext != null && !ext.isEmpty())
            {
                if (!TextUtil.isEmpty(ext.get("image_URLs")))
                {
                    String carPic = ext.get("image_URLs");
                    String[] strings = carPic.split(";");
                    if (strings.length == 1)
                    {
                        return;
                    }
                    if (strings.length == 2)
                    {
                        carPic1 = strings[0];
                        carPic2 = strings[1];
                    }
                }
            }
            String c1 = ImageUtil.imageToBase64(carPic1);
            String c2 = ImageUtil.imageToBase64(carPic2);
            //新建ws客户端
            SoapClient client = SoapClient.create(Config.webserviceUrl)
                    // 设置要请求的方法，传入对应的命名空间
                    .setMethod("SendAlarmPass", Config.nameSpaceUrl)
                    // 设置参数
                    .setParam("VehicleAlarmInfo", hk.toString())
                    .setParam("PlatePicData", null)
                    .setParam("CarPic1", c1)
                    .setParam("CarPic2", c2)
                    .setParam("CarPic3", null);
            // 发送请求，参数true表示返回一个格式化后的XML内容
            // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
            String result = client.send(true);
            logger.info("返回数据：{}", result);
            Document docResult = XmlUtil.readXML(result);
            Object value = XmlUtil.getByXPath("//ResPond", docResult, XPathConstants.STRING);
            Document doc = XmlUtil.readXML((String) value);
            Object CODE = XmlUtil.getByXPath("//CODE", doc, XPathConstants.STRING);
            Object MESSAGE = XmlUtil.getByXPath("//MESSAGE", doc, XPathConstants.STRING);
            if ("0".equals(CODE))
            {
                logger.info("上传成功，违法车牌号为：{}，违法时间为：{}", vv.getPlateNbr(), vv.getCaptureTime());
            }
            else
            {
                logger.error("上传失败，失败原因：{}", MESSAGE);
            }
        }
        catch (Exception e)
        {
            logger.error("上传数据出错：", e);
        }
    }
}
